<?php
/**
 * @attention Ne pas oublier de remettre la position de la planète dans sont état ou faire U1_Research::setPositionPlanet avant de faire des opérations sur les recherches.
 * @author jonathan <jo.link.noir@gmail.com>
 * @version 1.0: jo_link_noir
 * @date 09-03-2010
 */
 class U1_Research_Element extends SW_Structure_Subject_Element
 {
	protected $percent;

	protected $gain;

	/** @var gain obtenu pour $duration = null*/
	protected $gainForTheDuration = null;

	/** @var pourcent à la fin de $duration = null*/
	protected $percentByTheEndOfTheDuration = null;

	/** @var level à la fin de $duration = null*/
	protected $levelByTheEndOfTheDuration = null;

	/** @var int : id de la planète sélectionner*/
	protected $idPlanet = 0;


	/** @var array : {id_planet => {g => {}, p => {}, unblock => {}, l => {}, price => {}}, ... }*/
	private $_storagePlanet = array();


	/**
	 * @param int $id
	 * @param int $group
	 * @param int $gain = 0
	 * @param int $duration = null
	 * @param array $price = null
	 */
	public function __construct($id, $group, $gain=0, $duration = null, array $price = null, $time_end = null)
	{
		$this->gain = $gain;
		$percent = U1_Research::calculatePercent($this);
		$this->percent = $percent % 100;
		parent::__construct($id, $group, $percent - $this->percent, $duration, $price, $time_end);
	}

	/**
	 * @param int $percent : si plus grand que 100 alors le niveau augmente
	 */
	public function setPercent($percent)
	{
		if ($percent >= 100)
		{
			$this->percent = $percent % 100;
			$this->level += $percent - $this->percent;
		}
		else
			$this->percent = $percent;
		$this->notify();
	}

	/**
	 * @return int
	 */
	public function getPercent()
	{
		return $this->percent;
	}

	/**
	 * @return int
	 */
	public function getGain()
	{
		return $this->gain;
	}

	/**
	 * @param int $gain
	 */
	public function setGain($gain)
	{
		$this->gain = $gain;
		$percent = U1_Research::calculatePercent($this);
		$this->percent = $percent % 100;
		$this->level = $percent - $this->percent;
		$this->notify();
	}

	/**
	 * @param int $level : niveau réel (niveau + pourcent)
	 */
	public function setRealLevel($level)
	{
		$this->percent = $level % 100;
		$this->level = $level - $this->percent;
		$this->notify();
	}

	/**
	 * @return int : niveau réel (niveau + percent)
	 */
	public function getRealLevel()
	{
		return $this->level*100 + $this->percent;
	}

	/**
	 * @param int $gain
	 * @return #field
	 */
	public function setGainForTheDuration($gain)
	{
		$this->gainForTheDuration = $gain;
		return $this;
	}

	/**
	 * @return int|null
	 */
	public function getGainForTheDuration()
	{
		return $this->gainForTheDuration;
	}

	/**
	 * @param int $percent : si plus grand que 100 alors le niveau augmente
	 * @return #field
	 */
	public function setPercentByTheEndOfTheDuration($percent)
	{
		$this->percentByTheEndOfTheDuration = $percent % 100;
		$this->levelByTheEndOfTheDuration = $percent - $this->percentByTheEndOfTheDuration;
		return $this;
	}

	/**
	 * @return int|null
	 */
	public function getPercentByTheEndOfTheDuration()
	{
		return $this->percentByTheEndOfTheDuration;
	}

	/**
	 * @return int|null
	 */
	public function getLevelByTheEndOfTheDuration()
	{
		return $this->levelByTheEndOfTheDuration;
	}

	/**
	 * Invalide $percentForTheDuration et $levelForTheDuration
	 * @param int $duration : en heure
	 */
	public function setDuration($duration)
	{
		$this->duration = (int)$duration*3600;
		$this->gainForTheDuration = $this->percentByTheEndOfTheDuration = $this->levelByTheEndOfTheDuration = null;
		return $this;
	}

	/**
	 * @param int $id_planet
	 * @return #field
	 */
	public function setPositionPlanet($id_planet)
	{
		$id_planet = (int)$id_planet;
		if ($this->idPlanet !== $id_planet)
		{
			if (!isset($this->_storagePlanet[$id_planet]))
			{
				$this->_storagePlanet[$id_planet] = array(
					'duration' => 0,
					'price' => array(),
					'g' => null,
					'p' => null,
					'l' => null
				);
			}
			$storage = &$this->_storagePlanet[$id_planet];

			$this->idPlanet = $id_planet;
			$this->duration = &$storage['duration'];
			$this->price	= &$storage['price'];
			$this->gainForTheDuration			= &$storage['g'];
			$this->percentByTheEndOfTheDuration	= &$storage['p'];
			$this->levelByTheEndOfTheDuration	= &$storage['l'];
		}

		return $this;
	}

	/**
	 * @return int
	 */
	public function getPositionPlanet()
	{
		return $this->idPlanet;
	}
 }